package com.atguigu.eduservice.controller.front;

import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.TeacherQuery;
import com.atguigu.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-02-24
 */
@Api(description = "讲师管理-前台")
@RestController
@RequestMapping("/eduservice/teacher/front")
@CrossOrigin
public class TeacherController {

    @Resource
    private EduTeacherService eduTeacherService;

    @ApiOperation("获取讲师信息")
    @PostMapping("/info/{id}")
    public R getTeacherInfo(@PathVariable("id") String id) {
        Map<String, Object> result = eduTeacherService.getTeacherInfo(id);
        return R.ok().data(result);
    }

    @ApiOperation("前台分页获取讲师")
    @PostMapping("/page")
    public R pageTeacher(@RequestBody TeacherQuery query) {
        int pageNum = query.getPageNum();
        int pageSize = query.getPageSize();
        Page<EduTeacher> page = new Page<>(pageNum, pageSize);
        eduTeacherService.lambdaQuery()
                .select(EduTeacher::getId, EduTeacher::getAvatar, EduTeacher::getName, EduTeacher::getIntro, EduTeacher::getLevel)
                .orderByAsc(EduTeacher::getSort).page(page);
        return R.ok().data("teachers", page.getRecords())
                .data("total", page.getTotal())
                .data("hasNext", page.hasNext())
                .data("hasPrevious", page.hasPrevious());
    }


}
